package com.party.web.biz.merchantAnalyze;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.party.common.utils.BigDecimalUtils;
import com.party.common.utils.DateUtils;
import com.party.core.model.commune.CDataAnalysisCount;
import com.party.core.model.commune.CDataAnalysisCountOutput;
import com.party.core.model.commune.CDataAnalysisDetailOutput;
import com.party.core.model.commune.CDateAnalysisIndexViewOutput;
import com.party.core.model.member.MemberMerchantCount;
import com.party.core.model.member.MemberMerchantDetail;
import com.party.core.model.member.MemberMerchantDetailQuery;
import com.party.core.service.commune.ICDataAnalysisCountService;
import com.party.core.service.commune.ICDataAnalysisDetailService;
import com.party.core.service.commune.impl.CDataAnalysisCountService;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.member.IMemberMerchantCountService;
import com.party.core.service.member.IMemberMerchantDetailService;
import com.party.web.web.dto.output.merchantAnalyze.MerchantAnalyzeOutput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import static com.party.common.utils.DateUtils.addMonth;

/**
 * 合作商统计业务接口
 * Created by wei.li.
 *
 * @date 2017/12/14 0014
 * @time 15:08
 */

@Service
public class MemberMerchantDetailBizService {


    @Autowired
    private IMemberMerchantDetailService memberMerchantDetailService;

    @Autowired
    private IMemberMerchantCountService memberMerchantCountService;

    @Autowired
    private IMemberGroupService memberGroupService;

    @Autowired
    private ICDataAnalysisDetailService cDataAnalysisDetailService;

    @Autowired
    private ICDataAnalysisCountService cDataAnalysisCountService;

    /**
     * 数据统计详情
     *
     * @param memberId 会员编号
     * @return 统计数据
     */
    public HashMap<String, MerchantAnalyzeOutput> detail(String memberId) {
        HashMap<String, MerchantAnalyzeOutput> map = Maps.newHashMap();
        String now = DateUtils.todayDate();
        MemberMerchantDetailQuery memberMerchantDetailQuery = new MemberMerchantDetailQuery();
        memberMerchantDetailQuery.setMemberId(memberId);
        memberMerchantDetailQuery.setCreateStart(now);
        memberMerchantDetailQuery.setCreateEnd(now);

        boolean isQueryChild = memberGroupService.isQueryChild(memberId);
        memberMerchantDetailQuery.setIsQueryChild(isQueryChild ? 1 : 0);

        //今天
        MemberMerchantDetail today = memberMerchantDetailService.sum(memberMerchantDetailQuery);

        //昨日
        String day = DateUtils.formatDate(DateUtils.addDay(now, -1));
        memberMerchantDetailQuery.setCreateStart(day);
        memberMerchantDetailQuery.setCreateEnd(day);
        MemberMerchantDetail yesterday = memberMerchantDetailService.sum(memberMerchantDetailQuery);

        //前日
        day = DateUtils.formatDate(DateUtils.addDay(now, -2));
        memberMerchantDetailQuery.setCreateStart(day);
        memberMerchantDetailQuery.setCreateEnd(day);
        MemberMerchantDetail beforeYDay = memberMerchantDetailService.sum(memberMerchantDetailQuery);

        //本周
        memberMerchantDetailQuery.setCreateStart(DateUtils.formatDate(DateUtils.getFirstDayOfWeek(0)));
        memberMerchantDetailQuery.setCreateEnd(DateUtils.formatDate(DateUtils.getLastDayOfWeek(0)));
        MemberMerchantDetail week = memberMerchantDetailService.sum(memberMerchantDetailQuery);

        //本月
        memberMerchantDetailQuery.setCreateStart(DateUtils.formatDate(DateUtils.getFirstDayOfMonth(0)));
        memberMerchantDetailQuery.setCreateEnd(DateUtils.formatDate(DateUtils.getLastDayOfMonth(0)));
        MemberMerchantDetail month = memberMerchantDetailService.sum(memberMerchantDetailQuery);

        //历史
        MemberMerchantCount history = memberMerchantCountService.findByMemberIdSum(memberId, isQueryChild);

        MerchantAnalyzeOutput total = new MerchantAnalyzeOutput();
        if (history != null) {
            total.setMoney(BigDecimalUtils.round(history.getMoney(), 2));
        } else {
            history = new MemberMerchantCount();
            history.setApplyNum(0);
            history.setMoney(0d);
            history.setOrderNum(0);
            history.setRealMemberNum(0);
            history.setViewNum(0L);
            history.setMemberNum(0);

            total.setMoney(0d);
        }

        map.put("today", MerchantAnalyzeOutput.transform(today));
        map.put("yesterday", MerchantAnalyzeOutput.transform(yesterday));
        map.put("beforeYDay", MerchantAnalyzeOutput.transform(beforeYDay));
        map.put("week", MerchantAnalyzeOutput.transform(week));
        map.put("month", MerchantAnalyzeOutput.transform(month));
        map.put("history", MerchantAnalyzeOutput.transform(history));
        map.put("total", total);
        return map;
    }

    /**
     * 行知公社-数据统计详情
     *
     * @param memberGroupId 会员编号
     * @return 统计数据
     */
    public HashMap<String, CDateAnalysisIndexViewOutput> cdataAnalyzeDetail(String memberGroupId) {
        HashMap<String, CDateAnalysisIndexViewOutput> map = Maps.newHashMap();
        HashMap<String, Object> params = Maps.newHashMap();

        List<String> memberGroupIds = Lists.newArrayList();
        memberGroupIds.add(memberGroupId);
        params.put("memberGroupIds", memberGroupIds);

        //今天
        String now = DateUtils.todayDate();
        params.put("createStart", now);
        params.put("createEnd", now);
        CDataAnalysisDetailOutput today = cDataAnalysisDetailService.sumOrg(params);

        //昨日
        String day = DateUtils.formatDate(DateUtils.addDay(now, -1));
        params.put("createStart", day);
        params.put("createEnd", day);
        CDataAnalysisDetailOutput yesterday = cDataAnalysisDetailService.sumOrg(params);

        //前日
        day = DateUtils.formatDate(DateUtils.addDay(now, -2));
        params.put("createStart", day);
        params.put("createEnd", day);
        CDataAnalysisDetailOutput beforeYDay = cDataAnalysisDetailService.sumOrg(params);

        //本周
        params.put("createStart", DateUtils.formatDate(DateUtils.getFirstDayOfWeek(0)));
        params.put("createEnd", DateUtils.formatDate(DateUtils.getLastDayOfWeek(0)));
        CDataAnalysisDetailOutput week = cDataAnalysisDetailService.sumOrg(params);

        //本月
        params.put("createStart", DateUtils.formatDate(DateUtils.getFirstDayOfMonth(0)));
        params.put("createEnd", DateUtils.formatDate(DateUtils.getLastDayOfMonth(0)));
        CDataAnalysisDetailOutput month = cDataAnalysisDetailService.sumOrg(params);

        //历史
        params.put("createStart", null);
        params.put("createEnd", null);
        CDataAnalysisCountOutput history_val = cDataAnalysisCountService.sumOrg(params);
        CDataAnalysisDetailOutput history = CDataAnalysisDetailOutput.transform(history_val);

        //历史_old
        HashMap<String, MerchantAnalyzeOutput> merchantAnalyzeMap = Maps.newHashMap();
        boolean isQueryChild = memberGroupService.isQueryChild(memberGroupId);
        MemberMerchantCount history_old = memberMerchantCountService.findByMemberIdSum(memberGroupId, isQueryChild);
        MemberMerchantCount total = new MemberMerchantCount();
        if (history_old != null) {
            total.setMoney(BigDecimalUtils.round(history_old.getMoney(), 2));
        } else {
            history_old = new MemberMerchantCount();
            history_old.setApplyNum(0);
            history_old.setMoney(0d);
            history_old.setOrderNum(0);
            history_old.setRealMemberNum(0);
            history_old.setViewNum(0L);
            history_old.setMemberNum(0);

            total.setMoney(0d);
        }

        map.put("today", new CDateAnalysisIndexViewOutput(today, null));
        map.put("yesterday", new CDateAnalysisIndexViewOutput(yesterday, null));
        map.put("beforeYDay", new CDateAnalysisIndexViewOutput(beforeYDay, null));
        map.put("week", new CDateAnalysisIndexViewOutput(week, null));
        map.put("month", new CDateAnalysisIndexViewOutput(month, null));
        map.put("history", new CDateAnalysisIndexViewOutput(history, null));
        map.put("history_old", new CDateAnalysisIndexViewOutput(null, history_old));
        map.put("total", new CDateAnalysisIndexViewOutput(null, total));
        return map;
    }

    /**
     * 时间列表
     *
     * @param length 长度
     * @return 时间
     */
    public List<Date> dateList(Integer length) {
        length = null == length ? 7 : length;
        Date now = new Date();
        Date startDate = null;
        if (length == 30) {
            startDate = addMonth(DateUtils.getTodayDate(), -1);
        } else {
            startDate = DateUtils.addDay(now, -(length - 1));
        }
        List<Date> dateList = DateUtils.getDateDifference(startDate, now);
        return dateList;
    }

    /**
     * 时间列表
     *
     * @param length 长度
     * @return 时间
     */
    public List<String> dateStringList(Integer length) {
        List<String> dateList = Lists.newArrayList();
        //按周计算
        if (0 == length) {
            dateList = Lists.newArrayList("第1周", "第2周", "第3周", "第4周", "第5周",
                    "第6周", "第7周", "第8周", "第9周", "第10周", "第11周(上周)", "第12周(本周)");
        }
        //按月计算
        else if (-1 == length) {
            for (int i = 11; i >= 0; i--) {
                String date = DateUtils.formatDate(DateUtils.getFirstDayOfMonth(-i), DateUtils.MONTH_PATTERN);
                dateList.add(date);
            }
        } else if (-2 == length) {
            for (int i = 23; i >= 0; i--) {
                String date = DateUtils.formatDate(DateUtils.getFirstDayOfMonth(-i), DateUtils.MONTH_PATTERN);
                dateList.add(date);
            }
        } else {
            List<Date> list = dateList(length);
            for (Date date : list) {
                dateList.add(DateUtils.formatDate(date));
            }
        }
        return dateList;
    }

    /**
     * 获取时间单元图表
     *
     * @param length 长度
     * @param type   类型
     * @return 数据
     */
    public List<String> chartListForUnit(Integer length, Integer type, String memberId) {
        String startDay;
        String endDay;

        List<String> list = Lists.newArrayList();

        int runNum = length == -2 ? 23 : 11;

        for (int i = runNum; i >= 0; i--) {
            startDay = 0 == length ? DateUtils.formatDate(DateUtils.getFirstDayOfWeek(-i))
                    : DateUtils.formatDate(DateUtils.getFirstDayOfMonth(-i));

            endDay = 0 == length ? DateUtils.formatDate(DateUtils.getLastDayOfWeek(-i))
                    : DateUtils.formatDate(DateUtils.getLastDayOfMonth(-i));

            MemberMerchantDetailQuery query = new MemberMerchantDetailQuery();
            query.setCreateStart(startDay);
            query.setCreateEnd(endDay);
            query.setMemberId(memberId);
            MemberMerchantDetail memberMerchantDetail = memberMerchantDetailService.sum(query);

            Object num;
            if (type == 0) {
                num = memberMerchantDetail.getApplyNum();
            } else if (type == 1) {
                num = memberMerchantDetail.getOrderNum();
            } else if (type == 2) {
                num = memberMerchantDetail.getMoney();
            } else if (type == 3) {
                num = memberMerchantDetail.getViewNum();
            } else if (type == 4) {
                num = memberMerchantDetail.getMemberNum();
            } else {
                num = memberMerchantDetail.getApplyNum();
            }
            list.add(num.toString());
        }
        return list;
    }

    /**
     * 获取时间单元图表
     *
     * @param length 长度
     * @param type   类型
     * @return 数据
     */
    public List<String> communityChartListForUnit(Integer length, Integer type, List<String> memberGroupIds) {
        String startDay;
        String endDay;

        List<String> list = Lists.newArrayList();

        int runNum = length == -2 ? 23 : 11;

        for (int i = runNum; i >= 0; i--) {
            startDay = 0 == length ? DateUtils.formatDate(DateUtils.getFirstDayOfWeek(-i))
                    : DateUtils.formatDate(DateUtils.getFirstDayOfMonth(-i));

            endDay = 0 == length ? DateUtils.formatDate(DateUtils.getLastDayOfWeek(-i))
                    : DateUtils.formatDate(DateUtils.getLastDayOfMonth(-i));

            HashMap<String, Object> params = Maps.newHashMap();
            params.put("createStart",startDay);
            params.put("createEnd",endDay);
            params.put("memberGroupIds",memberGroupIds);

            CDataAnalysisDetailOutput cDataAnalysisDetailOutput = cDataAnalysisDetailService.sumOrg(params);

            Object num = 0;
            if (type == 0) {
                num = cDataAnalysisDetailOutput.getMemberTotal();
            } else if (type == 1) {
                num = cDataAnalysisDetailOutput.getEffectiveMemberNum();
            } else if (type == 2) {
                num = cDataAnalysisDetailOutput.getActiveMemberNum();
            } else if (type == 3) {
                num = cDataAnalysisDetailOutput.getJobActiveMemberNum();
            } else if (type == 4) {
                num = cDataAnalysisDetailOutput.getRecommendMemberNum();
            } else if (type == 5) {
                num = cDataAnalysisDetailOutput.getIndustryNum();
            } else if (type == 6) {
                num = cDataAnalysisDetailOutput.getCompanyMoney();
            }
            list.add(num.toString());
        }
        return list;
    }

    /**
     * 数据列表
     *
     * @param length   时间长度
     * @param memberId 会员编号
     * @return 统计列表
     */
    public List<MemberMerchantDetail> lengthList(Integer length, String memberId) {
        String now = DateUtils.todayDate();
        String startDay = null;
        if (length == 30) {
            startDay = DateUtils.formatDate(addMonth(DateUtils.getTodayDate(), -1));
        } else {
            startDay = DateUtils.formatDate(DateUtils.addDay(now, -(length - 1)));
        }

        MemberMerchantDetailQuery memberMerchantDetailQuery = new MemberMerchantDetailQuery();
        memberMerchantDetailQuery.setMemberId(memberId);
        memberMerchantDetailQuery.setCreateStart(startDay);
        memberMerchantDetailQuery.setCreateEnd(now);

        boolean isQueryChild = memberGroupService.isQueryChild(memberId);
        memberMerchantDetailQuery.setIsQueryChild(isQueryChild ? 1 : 0);
        return memberMerchantDetailService.queryList(memberMerchantDetailQuery);
    }

    /**
     * 数据列表
     *
     * @param length         时间长度
     * @param memberGroupIds 会员编号
     * @return 统计列表
     */
    public List<CDataAnalysisDetailOutput> communityLengthList(Integer length, List<String> memberGroupIds) {
        String now = DateUtils.todayDate();
        String startDay = null;
        if (length == 30) {
            startDay = DateUtils.formatDate(addMonth(DateUtils.getTodayDate(), -1));
        } else {
            startDay = DateUtils.formatDate(DateUtils.addDay(now, -(length - 1)));
        }
        HashMap<String, Object> params = Maps.newHashMap();
        params.put("memberGroupIds",memberGroupIds);
        params.put("createStart",startDay);
        params.put("createEnd",now);
        return cDataAnalysisDetailService.sumOrgList(params);
    }

    /**
     * 图表统计数据
     *
     * @param type   类型
     * @param length 长度
     * @param list   列表
     * @return 统计数据
     */
    public List<String> chartList(Integer type, Integer length, List<MemberMerchantDetail> list) {
        List<Date> dateList = this.dateList(length);
        List<String> resultList = Lists.newArrayList();
        for (Date date : dateList) {
            Object num = 0;
            for (MemberMerchantDetail memberMerchantDetail : list) {
                if (DateUtils.formatDate(date).equals(DateUtils.formatDate(memberMerchantDetail.getCreateDate()))) {
                    if (type == 0) {
                        num = memberMerchantDetail.getApplyNum();
                    } else if (type == 1) {
                        num = memberMerchantDetail.getOrderNum();
                    } else if (type == 2) {
                        num = memberMerchantDetail.getMoney();
                    } else if (type == 3) {
                        num = memberMerchantDetail.getViewNum();
                    } else if (type == 4) {
                        num = memberMerchantDetail.getMemberNum();
                    } else {
                        num = memberMerchantDetail.getApplyNum();
                    }
                    break;
                }
            }
            resultList.add(num.toString());
        }
        return resultList;
    }

    /**
     * 图表统计数据
     *
     * @param type   类型
     * @param length 长度
     * @param list   列表
     * @return 统计数据
     */
    public List<String> communityChartList(Integer type, Integer length, List<CDataAnalysisDetailOutput> list) {
        List<Date> dateList = this.dateList(length);
        List<String> resultList = Lists.newArrayList();
        for (Date date : dateList) {
            Object num = 0;
            for (CDataAnalysisDetailOutput cDataAnalysisDetailOutput : list) {
                if (DateUtils.formatDate(date).equals(DateUtils.formatDate(cDataAnalysisDetailOutput.getCreateDate()))) {
                    if (type == 0) {
                        num = cDataAnalysisDetailOutput.getMemberTotal();
                    } else if (type == 1) {
                        num = cDataAnalysisDetailOutput.getEffectiveMemberNum();
                    } else if (type == 2) {
                        num = cDataAnalysisDetailOutput.getActiveMemberNum();
                    } else if (type == 3) {
                        num = cDataAnalysisDetailOutput.getJobActiveMemberNum();
                    } else if (type == 4) {
                        num = cDataAnalysisDetailOutput.getRecommendMemberNum();
                    } else if (type == 5) {
                        num = cDataAnalysisDetailOutput.getIndustryNum();
                    } else if (type == 6) {
                        num = cDataAnalysisDetailOutput.getCompanyMoney();
                    }
                    break;
                }
            }
            resultList.add(num.toString());
        }
        return resultList;
    }

    /**
     * 单个图表数据
     *
     * @param length   长度
     * @param memberId 会员编号
     * @param type     类型
     * @return 统计数据
     */
    public List<String> oneChartList(Integer length, String memberId, Integer type) {
        if (0 == length || -1 == length || -2 == length) {
            return this.chartListForUnit(length, type, memberId);
        }
        List<MemberMerchantDetail> list = this.lengthList(length, memberId);
        return this.chartList(type, length, list);
    }

    /**
     * 单个图表数据
     *
     * @param length   长度
     * @param memberGroupIds 会员编号
     * @param type     类型
     * @return 统计数据
     */
    public List<String> communityOneChartList(Integer length, List<String> memberGroupIds, Integer type) {
        if (0 == length || -1 == length || -2 == length) {
            return this.communityChartListForUnit(length, type, memberGroupIds);
        }
        List<CDataAnalysisDetailOutput> list = this.communityLengthList(length, memberGroupIds);
        return this.communityChartList(type, length, list);
    }


    /**
     * 更新报名数
     *
     * @param memberId 会员编号
     */
    public void updateApplyNum(String memberId) {
        if (!Strings.isNullOrEmpty(memberId)) {
            MemberMerchantCount memberMerchantCount = memberMerchantCountService.getSet(memberId);
            if (null != memberMerchantCount.getApplyNum()) {
                memberMerchantCount.setApplyNum(memberMerchantCount.getApplyNum() + 1);
            } else {
                memberMerchantCount.setApplyNum(1);
            }
            memberMerchantCountService.update(memberMerchantCount);

            MemberMerchantDetail memberMerchantDetail = memberMerchantDetailService.getSet(memberId, DateUtils.today());
            if (null != memberMerchantDetail.getApplyNum()) {
                memberMerchantDetail.setApplyNum(memberMerchantDetail.getApplyNum() + 1);
            } else {
                memberMerchantDetail.setApplyNum(1);
            }
            memberMerchantDetailService.update(memberMerchantDetail);

        }
    }
}
